package com.company.cloud.acct.modules.mgr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.acct.modules.api.constant.AcctStateEnum;
import com.company.cloud.acct.modules.api.response.AccountModel;
import com.company.cloud.acct.modules.app.controller.req.AcctMergeParam;
import com.company.cloud.acct.modules.base.entity.AccountCashInfo;
import com.company.cloud.acct.modules.base.entity.AccountInfo;
import com.company.cloud.acct.modules.base.service.AccountInfoService;
import com.company.cloud.acct.modules.cache.models.AcctCashModel;
import com.company.cloud.acct.modules.cache.utils.AcctConvertUtils;
import com.company.cloud.acct.modules.mgr.controller.req.AcctQueryParam;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.function.Function;

@Authc
@RestController
@RequestMapping("/mgr/acct/info")
@Api(tags="Acct_MGR_账户管理")
public class AcctInfoMgrController {

    @Autowired
    private AccountInfoService accountInfoService;


    @PostMapping("/query")
    @ApiOperation(value="账户信息列表",response = AccountModel.class)
    public BaseResult query(@RequestBody AcctQueryParam params) {
        ValidatorUtils.validate(params);
//        Set<Integer> userIds = brokerRemoteService.getUserIdsByName(params.getUserName()).getData();

        LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
        IPage<AccountInfo> page = accountInfoService.page(new Page<>(params.getPage(), params.getRows()),
                new QueryWrapper<AccountInfo>()
                .eq(CheckUtils.hasInt(params.getUserId()),"user_id",userInfo.getId())
//                .in(CheckUtils.hasText(params.getUserName()), "user_id", userIds)
                .in("type",params.getTypes())
        );
        IPage<AccountModel> data = page.convert(new Function<AccountInfo, AccountModel>() {
            @Override
            public AccountModel apply(AccountInfo entity) {
                return AcctConvertUtils.getAccountModel(entity);
            }
        });
        return BaseResult.ok(data);
    }

    @GetMapping("/get")
    @ApiOperation(value="账户信息列表",response = AccountModel.class)
    public BaseResult query(@RequestParam("id") Integer id) {
        AccountInfo entity = accountInfoService.getById(id);
        return BaseResult.ok(AcctConvertUtils.getAccountModel(entity));
    }

}
